<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="com.uc.training.smadmin.gds.dao.CategoryDao">
    <!--查找分类-->
    <select id="getCategoryList" resultClass="com.uc.training.smadmin.gds.re.CategoryRE">
        SELECT
        t.id AS id,
        t.name AS name,
        t.sort_num AS sortNum,
        t.parent_id As parentId,
        t.image_url AS imageUrl
        FROM t_gds_category t
        WHERE t.is_delete = 0
        order by  t.sort_num ASC
    </select>
    <!--根据主键id查询分类-->
    <select id="queryCategory" resultClass="com.uc.training.smadmin.gds.model.Category" parameterClass="Long">
        SELECT
        t.id AS id,
        t.name AS name,
        t.sort_num AS sortNum,
        t.is_delete AS isDelete,
        t.parent_id As parentId
        FROM t_gds_category t
        WHERE id = #id#
    </select>
    <!--根据父类id查找子分类id-->
    <select id="queryIdByParentId" parameterClass="Long" resultClass="Long">
        SELECT
            t.id AS id
        FROM t_gds_category t
        WHERE t.is_delete = 0 AND t.parent_id = #id#
    </select>

    <!--查找相同名称的数量-->
    <select id="getCountByNameAndParentId" parameterClass="com.uc.training.smadmin.gds.vo.CategoryVO" resultClass="Integer">
        SELECT
        count(1)
        FROM t_gds_category t
        WHERE t.name = #name# AND t.parent_id = #parentId# AND t.is_delete = 0
    </select>

    <!--新增分类-->
    <insert id="addCategory" parameterClass="com.uc.training.smadmin.gds.model.Category">
        insert into t_gds_category
        (`name`, sort_num, parent_id, is_delete, image_url, create_emp, modify_emp)
        values(#name#,#sortNum#,#parentId#,0,#imageUrl#,#createEmp#,#modifyEmp#)
        <selectKey resultClass="long" keyProperty="id">
            SELECT LAST_INSERT_ID() as id;
        </selectKey>
    </insert>

    <!--更新分类-->
    <update id="updateCategory" parameterClass="com.uc.training.smadmin.gds.model.Category">
        UPDATE t_gds_category
        SET  `name`=#name#, sort_num=#sortNum#, image_url=#imageUrl#, modify_emp=#modifyEmp#
        WHERE id = #id#
    </update>

    <!--逻辑删除-->
    <update id="logicDeleteCategory" parameterClass="Long">
        UPDATE t_gds_category
        SET  is_delete=1
        WHERE id = #id#
    </update>

</sqlMap>